using System;
using System.Collections.Generic;
using System.Globalization;
namespace Microsoft.Phone.Controls.Maps.Core
{
	internal static class ResourceUtility
	{
		private static readonly Dictionary<Type, Dictionary<CultureInfo, object>> cache = new Dictionary<Type, Dictionary<CultureInfo, object>>();
		private static bool TryToGetCultureInfo(string cultureName, out CultureInfo cultureInfo)
		{
			bool result;
			try
			{
				cultureInfo = new CultureInfo(cultureName);
				return true;
			}
			catch (ArgumentException)
			{
				cultureInfo = null;
				result = false;
			}
			return result;
		}
		internal static CultureInfo GetCultureInfo(string cultureName)
		{
			CultureInfo result;
			if (ResourceUtility.TryToGetCultureInfo(cultureName, out result))
			{
				return result;
			}
			int num = cultureName.IndexOf('-');
			if (num > 0)
			{
				string cultureName2 = cultureName.Substring(0, num);
				if (ResourceUtility.TryToGetCultureInfo(cultureName2, out result))
				{
					return result;
				}
			}
			return CultureInfo.get_CurrentUICulture();
		}
		private static bool TryToGetRegionInfo(string regionName, out RegionInfo regionInfo)
		{
			bool result;
			try
			{
				regionInfo = new RegionInfo(regionName);
				return true;
			}
			catch (ArgumentException)
			{
				regionInfo = null;
				result = false;
			}
			return result;
		}
		internal static RegionInfo GetRegionInfo(string regionName)
		{
			RegionInfo result;
			if (ResourceUtility.TryToGetRegionInfo(regionName, out result))
			{
				return result;
			}
			return RegionInfo.get_CurrentRegion();
		}
		internal static TResourceClass GetResource<TResourceClass, THelper>(CultureInfo culture) where TResourceClass : class where THelper : class, IResourceHelper<TResourceClass>, new()
		{
			Dictionary<CultureInfo, object> dictionary;
			if (!ResourceUtility.cache.TryGetValue(typeof(TResourceClass), ref dictionary))
			{
				dictionary = new Dictionary<CultureInfo, object>();
				ResourceUtility.cache.set_Item(typeof(TResourceClass), dictionary);
			}
			object obj;
			if (!dictionary.TryGetValue(culture, ref obj))
			{
				THelper tHelper = Activator.CreateInstance<THelper>();
				TResourceClass tResourceClass = tHelper.CreateResource();
				tHelper.SetResourceCulture(tResourceClass, culture);
				dictionary.set_Item(culture, tResourceClass);
				obj = tResourceClass;
			}
			return obj as TResourceClass;
		}
	}
}
